import { areaList } from '../../../area-data/index';
import {ajaxFun} from "../../../../utils/ajax"
Page({

  data: {
        id:"",//地址id
        isDefault:false,//是否默认
        realName:"",//姓名
        phone:"",//电话
        province:"",//省
        city:"",//市
        district:"",//区
        detail:"",//详细地址
        areaList,//****全国所有的省市区
        show:false//地址弹框
  },
  onLoad(options){
      if(options.address){//编辑有参数
        //把编辑页面带过来的参数 解构出来
        let {id,isDefault,realName,phone,province,city,district,detail}=JSON.parse(options.address);
         console.log("编辑");
         this.setData({id,isDefault,realName,phone,province,city,district,detail})
      }else{
        console.log("添加");
      }
  },
  changeDefault(){//切换默认地址

    this.setData({
      isDefault:!this.data.isDefault
    })
  },
  showAddress(){//显示弹框事件
    this.setData({
      show:true
    })
  },
  confirm(e){//确定地址
    console.log(e.detail);
   let arry=e.detail.values
    this.setData({
      show:false,
      province:arry[0].name,//省
      city:arry[1].name,//市
      district:arry[2].name,//区
    })
  },
  cancel(){//取消地址
    this.setData({
      show:false
    })
  },
  async chooseAddress(){//打开地图选择地址---es7写法
      let {latitude,longitude}=await wx.getLocation({type: 'wgs84',})
      let {name,address}=await wx.chooseLocation({latitude,longitude})
      this.setData({
        detail:address
      })
  },



  saveFun(){//立即保存按钮
      if(this.data.id){
          console.log("请求编辑接口");
          this.addEditorAddress(this.data.id)
      }else{
          console.log("请求添加接口");// id  0
          this.addEditorAddress(0)
      }
  },
 async addEditorAddress(id){
  let {province,city,district,detail,isDefault,phone,realName}=this.data; 
  let res=await ajaxFun("/address/edit","post",{
                address:{province, city, district, cityId:3},
                detail,
                id,
                isDefault,
                phone,
                realName
         })
         let  pages=getCurrentPages();
         let prepage=pages[pages.length-2];
         prepage.setData({
           list:[]
         },()=>{
           prepage.getList()
         wx.navigateBack({
           delta:1
         })
         })
         
  }
  
})